﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;

namespace Project5188.BUS.Operations
{
    public class IdCardInfos : DetailView<idcardinfo>
    {
        private project5188Entities _context;
        // ReSharper disable InconsistentNaming
        private new List<idcardinfo> EntityList;
        private new idcardinfo Entity;
        // ReSharper restore InconsistentNaming

        public long TransactionId { get; private set; }

        #region Overrides of DetailView<idcardinfo>

        public override List<idcardinfo> GetEntityList()
        {
            _context = new project5188Entities();
            EntityList = _context.idcardinfoes.Where(a => !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override List<idcardinfo> GetEntityById(long id)
        {
            _context = new project5188Entities();
            EntityList = _context.idcardinfoes.Where(a => a.a_ID == id && !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override idcardinfo GetEntity()
        {
            _context = new project5188Entities();
            Entity = new idcardinfo();
            return Entity;
        }

        public override void Update()
        {
            _context.SaveChanges();
        }

        public override void Insert()
        {
            if (ExistPersonnel(Entity.b_PersonnelID))
            {
                throw new Exception("Seçtiğiniz personel için sistemde kayıt mevcuttur.");
            }

            _context.AddToidcardinfoes(Entity);
            _context.SaveChanges();
            TransactionId = Entity.a_ID;
        }

        #endregion

        public bool ExistPersonnel(long personnelId)
        {
            _context = new project5188Entities();

            return _context.idcardinfoes.Where(a => a.b_PersonnelID == personnelId && !a.IsDeleted).Count() > 0;
        }

        public List<idcardinfo> GetEntityListByParameters(string registryNoText, string nameSurname, string tcNoText, DateTime? birthDate)
        {
            _context = new project5188Entities();
            var parameterList = new List<ObjectParameter>();

            var dynamicString = "it.IsDeleted = @IsDeleted";
            parameterList.Add(new ObjectParameter("IsDeleted", false));

            dynamicString += " And it.personnelinfo.IsDeleted = @PersonnelIsDeleted";
            parameterList.Add(new ObjectParameter("PersonnelIsDeleted", false));

            if (!string.IsNullOrEmpty(registryNoText) && !String.IsNullOrWhiteSpace(registryNoText))
            {
                dynamicString += " And it.personnelinfo.b_RegisterNo = @RegisterNo";
                parameterList.Add(new ObjectParameter("RegisterNo", Convert.ToInt32(registryNoText)));
            }

            if (!string.IsNullOrEmpty(nameSurname) && !string.IsNullOrWhiteSpace(nameSurname))
            {
                dynamicString += " And it.personnelinfo.f_NameSurname = @PersonnelNameSurname";
                parameterList.Add(new ObjectParameter("PersonnelNameSurname", nameSurname));
            }

            if (!string.IsNullOrEmpty(tcNoText) && !String.IsNullOrWhiteSpace(tcNoText))
            {
                dynamicString += " And it.c_TCNo = @TCNo";
                parameterList.Add(new ObjectParameter("TCNo", Convert.ToInt32(tcNoText)));
            }

            if (birthDate != null)
            {
                dynamicString += " And it.j_BirthDate = @BirthDate";
                parameterList.Add(new ObjectParameter("BirthDate", birthDate));
            }

            EntityList = _context.idcardinfoes.Where(dynamicString, parameterList.ToArray()).
                OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }
    }
}
